package com.song.JavaThread_day07.ch7_2;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

public class Main {
	
	
	public static void main(String[] args) {
		
		MyExecutor executor = new MyExecutor(2, 4, 1000, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>());
		
		List<Future<String>> results = new ArrayList<Future<String>>();
		
		for(int i=0;i<10;i++)
		{
			SleepTwoSecondsTask task = new SleepTwoSecondsTask();
			Future<String> result = executor.submit(task);
			results.add(result);
		}
		
		for(int i=0;i<5 ;i++)
		{
			try {
				String result = results.get(i).get();
				System.out.printf(" Main : result for Task %d %s \n",i,result);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		executor.shutdown();	
		
		for(int i=0;i<5 ;i++)
		{
			try {
				String result = results.get(i).get();
				System.out.printf(" Main : result for Task %d %s \n",i,result);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			executor.awaitTermination(1, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.printf(" Main : End of the program \n");
		
	}
	
}
